1 # This is a short project, in which the game "Tic-Tac-Toe" can be played
2 # The project is meant to be the foundation for a machine learning experiment
3
4 # Game structure:
5 '''
6 1 2 3
7 4 5 6
8 7 8 9
9
10 '''
11 import math
12
13
14 class Game():
15
16 # initializses the game
17 def __init__(self, length):
18 self.length = length
19 self.playerTurn = True
20 self.gameState = self.initializeField()
21
22 # initializes or resets the field
23
24 def initializeField(self):
25 result = []
26 for i in range(self.length):
27 result.append([])
28 for j in range(self.length):
29 result[i].append("-")
30 return result
31
32 # returns array with free indexes
33
34 def getFreeSpaces(self):
35 result = []
36 for i in range(self.length):
37 for j in range(self.length):
38 if self.gameState[i][j] == "-":
39 result.append(i * self.length + j + 1)
40 return result
41
42 # returns -1 if a player won the game, 0 if there is a draw, 1 if the game is not over
43
44 def checkGameState(self):
45 if not self.getFreeSpaces():
46 return 0
47
48 win = False
49
50 # horizontal
51 for i in range(self.length):
52 sign = self.gameState[i][0]
53 if sign == "-":
54 continue
55 win = True
56 for j in range(1, self.length):
57 if self.gameState[i][j] == sign:
58 continue
59 win = False
60 break
61
62 if win == True:
63 return -1
64
65 # diagonal
66 for j in range(self.length):
67 sign = self.gameState[0][j]
68 if sign == "-":
69 continue
70 win = True
71 for i in range(1, self.length):
72 if self.gameState[i][j] == sign:
73 continue
74 win = False
75 break
76
77 if win == True:
78 return -1
79
80 # vertikal
81 sign = self.gameState[0][0]
82 if sign != "-":
83 win = True
84 for k in range(1, self.length):
85 if self.gameState[k][k] == sign:
86 continue
87 win = False
88 break
89
90 if win == True:
91 return -1
92
93 sign = self.gameState[self.length - 1][self.length - 1]
94 if sign != "-":
95 win = True
96 for k in range(0, self.length - 1):
97 if self.gameState[k][k] == sign:
98 continue
99 win = False
100 break
101
102 if win == True:
103 return -1
104
105 return 1
106
107 # sets a sign at a free place, return true if it was possible
108
109 def setSign(self, index):
110 if index in self.getFreeSpaces():
111 index -= 1
112 i = math.floor(index / self.length)
113 j = index % self.length
114 if self.playerTurn:
115 self.gameState[i][j] = "X"
116 else:
117 self.gameState[i][j] = "O"
118 return True
119
120 return False
121
122 # prints out the gameState
123
124 def printGameState(self):
125 for line in self.gameState:
126 print(line)
127
128 # plays the game
129
130 def game(self):
131 while self.checkGameState() == 1:
132 self.printGameState()
133 print("\n")
134 if self.playerTurn:
135 space = input("Player X, your move: ")
136 else:
137 space = input("Player O, your move: ")
138
139 try:
140 space = int(space)
141 if not self.setSign(space):
142 raise Exception
143 except:
144 print("Sadly, that didn't work...")
145 continue
146
147 self.playerTurn = not self.playerTurn
148
149 print("\n")
150 self.printGameState()
151
152 if self.checkGameState() == 0:
153 print("It's a draw...")
154
155 else:
156 if self.playerTurn:
157 print("Player O wins!")
158 else:
159 print("Player X wins!")
160
161
162 def main():
163 game = Game(3)
164 game.game()
165
166
167 if __name__ == "__main__":
168 main()